<?php
// 初始化麻将牌
function initMahjong() {
    $suits = ['万', '条', '筒'];
    $numbers = range(1, 9);
    $wind = ['东', '南', '西', '北'];
    $dragon = ['中', '发', '白'];
    $mahjong = [];

    // 生成万、条、筒牌
    foreach ($suits as $suit) {
        foreach ($numbers as $number) {
            for ($i = 0; $i < 4; $i++) {
                $mahjong[] = $number . $suit;
            }
        }
    }

    // 生成风牌
    foreach ($wind as $w) {
        for ($i = 0; $i < 4; $i++) {
            $mahjong[] = $w;
        }
    }

    // 生成箭牌
    foreach ($dragon as $d) {
        for ($i = 0; $i < 4; $i++) {
            $mahjong[] = $d;
        }
    }

    return $mahjong;
}

// 洗牌函数
function shuffleMahjong(&$mahjong) {
    shuffle($mahjong);
}

// 发牌函数
function dealMahjong($mahjong) {
    $players = [
        'player1' => [],
        'player2' => [],
        'player3' => [],
        'player4' => []
    ];

    // 每人先发 13 张牌
    for ($i = 0; $i < 13; $i++) {
        foreach ($players as $playerName => &$hand) {
            $hand[] = array_shift($mahjong);
        }
    }

    return $players;
}

// 初始化麻将牌
$mahjong = initMahjong();
// 洗牌
shuffleMahjong($mahjong);
// 发牌
$players = dealMahjong($mahjong);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>麻将游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        .player {
            margin-bottom: 20px;
        }

        .player h2 {
            margin-bottom: 10px;
        }

        .card {
            display: inline-block;
            border: 1px solid #ccc;
            padding: 5px 10px;
            margin: 2px;
            background-color: #fff;
        }
    </style>
</head>

<body>
    <?php foreach ($players as $playerName => $hand) : ?>
        <div class="player">
            <h2><?php echo $playerName; ?></h2>
            <?php foreach ($hand as $card) : ?>
                <span class="card"><?php echo $card; ?></span>
            <?php endforeach; ?>
        </div>
    <?php endforeach; ?>
</body>

</html>